Functies zijn 'eersteklas burgers'
Home

Functies zijn 'eersteklas burgers'

Functies zijn 'eersteklas burgers'

Van een programmeertaal wordt gezegd dat ze eersteklas functies (first-class functions of literal functions) ondersteunt als het functies behandelt als eersteklas objecten. Concreet betekent dit dat de taal:

Inleiding

Het begrip "eersteklas functies" werd geïntroduceerd door de Britse computerwetenschapper Christopher Strachey in de jaren 1960.

Filmpje: Functies zijn 'eersteklas burgers' in JavaScript

Dit is een belangrijk begrip van functioneel programmeren namelijk functies als eersterangs burgers. Michael Fogus definiëert eersterangs functies in zijn boek, Functional JavaScript, 2013, als volgt:

The term “first-class” means that something is just a value. A first-class function is one that can go anywhere that any other value can go—there are few to no restrictions. A number in JavaScript is surely a first-class thing, and therefore a first-class function has a similar nature:

Javascript functies zijn 'First-Class omdat':

Top-level functies zijn eigenschappen van het window object, net als alle andere globale variabelen. De volgende statements hebben allemaal de zelfde uitwerking:

function hallo() { alert('hallo') }
var hallo = function() { alert('hallo') }
window.hallo = function() { alert('hallo') }

Het feit dat functies eersteklas burgers zijn in JavaScript zorgt ervoor dat je 'rare' dingen kan doen in JavaScript.

Inhoudstafel

  1. Functies zijn data
  2. Anonieme functies
  3. Callback functies
  4. Functies die een functie retourneren
  5. Functies die zichzelf wijzigen
  6. Immediately-Invoked Function Expression
  7. Inner (private) functions
  8. Closures
  9. Closures in de praktijk

JI
2016-10-08 00:30:13